package com.bwie.service.lmpl;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.UserMapper;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: Sokai
 * @Date: 2024/9/25 20:15
 * @Description:
 **/
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public R userLogin(LoginInfoVo loginInfoVo) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUsername,loginInfoVo.getUserName());
        User user = userMapper.selectOne(wrapper);
        if (!user.getUsername().equals(loginInfoVo.getUserName())) {
            return R.ERROR(500,"账号不存在!");
        }
        if (!user.getPassword().equals(loginInfoVo.getUserPass())) {
            return R.ERROR(500,"密码错误!");
        }
        return R.OK();

    }

    @Override
    public R uploadFile(MultipartFile file) {


        try {
            String filename = file.getOriginalFilename();
            String extName = FileNameUtil.extName(filename);
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            e.printStackTrace();
            return R.ERROR(500,"上传失败");
        }

    }
}
